package com.yabushan.activiti.service;

import java.util.ArrayList;
import java.util.List;

import com.yabushan.activiti.domain.HistoryTaskInfo;
import com.yabushan.activiti.domain.NextStepAndUser;
import com.yabushan.activiti.util.Constant;
import com.yabushan.activiti.util.Utils;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class VacationServiceImpl implements  VacationService{

	@Autowired
	private HistoryQueryService historyQueryService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private EngineService engineService;
	@Autowired
	private workflowUtilService workflowUtilService;
	@Autowired
	private VariablesService variablesService;
	@Autowired
	private ProcessDefinitionService processDefinitionService;

	@Override
	public List<HistoryTaskInfo> getAllHistoryStep(String processInstanceId, boolean isFinish, Integer firstResult, Integer maxResults) {
		List<HistoryTaskInfo> result =new ArrayList<HistoryTaskInfo>();
		HistoryTaskInfo historyTaskInfo=null;
		//获取所有历史任务
		List<HistoricTaskInstance> list=historyQueryService.getHistoricTaskInstances(processInstanceId, isFinish, firstResult, maxResults);
				//flowEngineService.getHistoricProcessInstanceByProcessDefId(processDefId);
		if(list!=null && list.size()>0){
			//遍历所有历史任务
			for (HistoricTaskInstance historicTaskInstance : list) {
				historyTaskInfo=new HistoryTaskInfo();
				historyTaskInfo.setHistoricTaskInstance(historicTaskInstance);
				if(historicTaskInstance.getEndTime()!=null){
					//封装流程变量
				List<HistoricVariableInstance> historicVariableInstances=historyQueryService.getHistoricVariableInstancesByTaskId(historicTaskInstance.getId());
					for (HistoricVariableInstance historicVariableInstance : historicVariableInstances) {
						if(historicVariableInstance.getVariableName().equals(Constant.STEP_INFO)){
							historyTaskInfo.setInfo(historicVariableInstance.getValue()+"");
						}
					}
					//封装批注信息
					List<Comment> comments=commentService.getCommentByTaskId(historicTaskInstance.getId());
					if(comments!=null && comments.size()>0){
							historyTaskInfo.setTaskComment(comments);
					}else{
						historyTaskInfo.setTaskComment(null);
					}
				}else{
					historyTaskInfo.setInfo("");
					historyTaskInfo.setTaskComment(null);
				}
				result.add(historyTaskInfo);
			}
		}
		return result;
	}



	@Override
	public NextStepAndUser getFirstUser(String processKey, String userId, String empType, String processDefId, ProcessDefinitionEntity pd) {
		return null;
	}

	@Override
	public NextStepAndUser getFirstUser(String processKey, String userId, String empType, String businessKey) {
		return null;
	}


	@Override
	public String getRequestUserId(String taskId) {
		//获取流程发起人
		String requestUser =variablesService.getVariablesByTaskId(Constant.REQUEST_USER, taskId, false).toString();
		return requestUser;
	}

	@Override
	public NextStepAndUser findUserAndStep(String applyUserId, String processDefinitionId, String activityId) {
		return null;
	}

	/**
	 *
	 * @param list List<ActivityImpl>
	 * @param nodeId 用户节点ID
	 * @param isJumpStep 标识是否跳环节，1：是
	 * @return
	 */
	public NextStepAndUser getNextStepAndUserByNodeId(List<ActivityImpl> list,String nodeId,String isJumpStep){
		NextStepAndUser nextStepAndUser2 = new NextStepAndUser();
		for (ActivityImpl act1 : list) {
    			if(nodeId.equals(act1.getId())){//公司总经理审批
    				nextStepAndUser2.setNextStep(act1.getProperty("name").toString());
    				nextStepAndUser2.setJumpStep(isJumpStep);//是否跳过步骤，1：是
    				return nextStepAndUser2;
    			}
    	}
		return null;
	}

}
